//
//  MoreViewController.m
//  GameBoss
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MoreViewController.h"
#import "AFN_Get.h"
#import "MoreCell.h"
#import "UIImageView+WebCache.h"
#import "ArticleListModel.h"
#import "MJRefresh.h"
#import "DataViewController.h"
@interface MoreViewController ()
@property (nonatomic, retain) NSMutableArray *listArr;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation MoreViewController
- (void)dealloc
{
    [_listArr release];
    [_modelArr release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.listArr = [NSMutableArray array];
        self.modelArr = [NSMutableArray array];
    }
    return self;
}

#pragma mark 跳转到中心视图
- (void)leftButtonClick
{
    //返回上一页面
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma  mark 拼接数据 , 解析数据
- (void)PathData
{
    NSString *url = [NSString stringWithFormat:@"http://mrobot.pcgames.com.cn/v3/cms/channels/%@", self.num];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.listArr = responseObject[@"articleList"];
        for (NSInteger i = 0; i < self.listArr.count; i ++) {
            NSMutableDictionary *listDic = self.listArr[i];
            ArticleListModel *model = [[ArticleListModel alloc] init];
            [model setValuesForKeysWithDictionary:listDic];
            [self.modelArr addObject:model];
            [model release];
        }
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.tableView headerEndRefreshing];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen.width, 200 * Screen.height / 667)];
        label.text = @"暂无匹配条件";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18];
        label.textColor = [UIColor grayColor];
        [self.tableView addSubview:label];
        [label release];
    }];
    
//    [AFN_Get netWorkingTool:url block:^(id responseObject) {
//        self.listArr = responseObject[@"articleList"];
//        for (NSInteger i = 0; i < self.listArr.count; i ++) {
//            NSMutableDictionary *listDic = self.listArr[i];
//            ArticleListModel *model = [[ArticleListModel alloc] init];
//            [model setValuesForKeysWithDictionary:listDic];
//            [self.modelArr addObject:model];
//            [model release];
//        }
//        [self.tableView reloadData];
//        [self.tableView headerEndRefreshing];
//    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView release];

    #pragma mark 上拉刷新, 下拉加载
    [self addHeaderNew];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.modelArr.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *reuse22 = @"reuse";
    MoreCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse22];
    if (!cell) {
        cell = [[[MoreCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:reuse22] autorelease];
    }
    ArticleListModel *model = self.modelArr[indexPath.row];
    cell.titleL.text =model.title;
    cell.pubDateL.text =model.pubDate;
    cell.imageVS.image = [UIImage imageNamed:@"iconfont-liaotian-2"];
    cell.cmtCountL.text =[NSString stringWithFormat:@"%ld", (long)model.cmtCount];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"iconfont-yonghutouxiang"]];
    return cell;
}
/********************/
- (void)addHeaderNew
{
    __unsafe_unretained typeof(self)vc = self;
    [self.tableView addHeaderWithCallback:^{
        [self.modelArr removeAllObjects];
        [vc PathData];
        [vc.tableView reloadData];
    }];
    [self.tableView headerBeginRefreshing];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * Screen.height / 667;
}
#pragma mark 跳转详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DataViewController *dataVC = [[DataViewController alloc] init];
    ArticleListModel *model = self.modelArr[indexPath.row];
    dataVC.name = model.title;
    dataVC.art = model;
    [self.navigationController pushViewController:dataVC animated:YES];
    [dataVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 选取白黑两种模式
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setViewColorWithView:self.tableView];
    //    [NightManager setLabelColorWithLabel:self.title];
    
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setViewColorWithView:self.tableView];
    //    [NightManager setLabelColorWithLabel:self.title];
    
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
